<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

function make_image($id, $file_path, $base_url='', $type='1')
{
	$file = 'data/images/'.$file_path.image_path($id).$id."_{$type}.jpg";

	$file = is_file(ROOT_PATH.$file) ? $base_url.$file : '';

	return $file;
}

function get_movie_images($movie_id, $base_url='', $category_id='2')
{
	$results = get_movie_image_list($movie_id, $category_id);

	$image_list = array();
	if(check_array($results))
	{
		$image_1 = $image_2 = $image_3 = $image_o = '';

		$temp = array();
		foreach($results as $image)
		{
			$temp['id'] = $image['id'];
			$temp['image_1'] = make_image($image['id'], 'movie/', $base_url, '1');
			$temp['image_2'] = make_image($image['id'], 'movie/', $base_url, '2');
			if($category_id != '2')
			{
				$temp['image_3'] = make_image($image['id'], 'movie/', $base_url, '3');
			}
			$temp['image_o'] = make_image($image['id'], 'movie/', $base_url, 'o');
			$temp['desc'] = $image['desc'];

			$image_list[] = $temp;
		}
	}

	return $image_list;
}

function delete_movie_images($image_ids)
{
	$image_o = $image_file_1 = $image_file_1 = $image_file_3 = '';

	require_once ROOT_PATH.'lib/io.han.php';
	$IoHandler = new IoHandler();

	$ids = explode(',', $image_ids);
	foreach($ids as $id)
	{
		$image_1 = make_image($id, 'movie/', ROOT_PATH, '1');
		$image_2 = make_image($id, 'movie/', ROOT_PATH, '2');
		$image_3 = make_image($id, 'movie/', ROOT_PATH, '3');
		$image_o = make_image($id, 'movie/', ROOT_PATH, 'o');
			
		if(is_file($image_o))
		{
			$IoHandler->DeleteFile($image_1);
			$IoHandler->DeleteFile($image_2);
			$IoHandler->DeleteFile($image_3);
			$IoHandler->DeleteFile($image_o);
		}
	}

	delete_movie_image(array('ids'=>$image_ids));
}

function move_image_to_temp_path($field)
{
	require_once ROOT_PATH.'lib/io.han.php';
	$IoHandler = new IoHandler();

	$type = trim(strtolower(end(explode(".", $_FILES[$field]['name']))));
	if($type != 'gif' && $type != 'jpg' && $type != 'png')
	{
		return '图片格式不对';
	}

	$image_name = substr(md5($_FILES[$field]['name']),-10).".{$type}";
	$cache_image_path = ROOT_PATH . 'data/cache/temp_images/'.$image_name{0}.'/';
	$image_file = $cache_image_path . $image_name;

	if(!is_dir($cache_image_path))
	{
		$IoHandler->MakeDir($cache_image_path);
	}

	// upload to temp image path
	require_once ROOT_PATH.'lib/upload.han.php';
	$UploadHandler = new UploadHandler($_FILES,$cache_image_path,$field,true);
	$UploadHandler->setMaxSize(2048);
	$UploadHandler->setNewName($image_name);
	$result=$UploadHandler->doUpload();

	if($result && is_image($image_file))
	{
		return $image_file;
	}
	else
	{
		$IoHandler->RemoveDir($cache_image_path);
		return false;
	}
}

function insert_image_to_database($image_file, $mid, $cid)
{
	list($image_width, $image_height, $image_type, $image_attr) = getimagesize($image_file);
	$image_size = filesize($image_file);

	$request['movie_id'] = $mid;
	$request['category_id'] = $cid;
	$request['name'] = basename($image_file);
	$request['width'] = $image_width;
	$request['height'] = $image_width;
	$request['size'] = $image_size;

	$image_id = add_movie_image($request);
	if($image_id < 1)
	{
		return '插入数据库失败';
	}
	
	return $image_id;
}

function make_movie_thumb_image($image_id, $image_file, $cid)
{
	require_once ROOT_PATH.'lib/io.han.php';
	$IoHandler = new IoHandler();
		
	$image_path = ROOT_PATH.'data/images/movie/'.image_path($image_id);

	if(!is_dir($image_path))
	{
		$IoHandler->MakeDir($image_path);
	}

	$thumbwidth = $thumbheight = 0;
	$image_name = $image_path.$image_id . "_o.jpg";
	$image_file_1 = $image_path.$image_id . "_1.jpg";
	$image_file_2 = $image_path.$image_id . "_2.jpg";

	if($cid == 1)
	{ // hai bao
		$image_file_3 = $image_path.$image_id . "_3.jpg";

		list($thumbwidth, $thumbheight) = explode('*', SIZE_HAIBAO_1);
		$result = image_resize($image_file, $image_file_1, $thumbwidth, $thumbheight, true);
		if(!$result && !is_file($image_file_1)) @copy($image_file, $image_file_1);

		list($thumbwidth, $thumbheight) = explode('*', SIZE_HAIBAO_2);
		$result = image_resize($image_file, $image_file_2, $thumbwidth, $thumbheight, true);
		if(!$result && !is_file($image_file_2)) @copy($image_file, $image_file_2);

		list($thumbwidth, $thumbheight) = explode('*', SIZE_HAIBAO_3);
		$result = image_resize($image_file, $image_file_3, $thumbwidth, $thumbheight, true);
		if(!$result && !is_file($image_file_3)) @copy($image_file, $image_file_3);
	}
	else if($cid == 2)
	{ // ju zhao
		list($thumbwidth, $thumbheight) = explode('*', SIZE_JUZHAO_1);
		$result = image_resize($image_file, $image_file_1, $thumbwidth, $thumbheight, true);
		if(!$result && !is_file($image_file_1)) @copy($image_file, $image_file_1);
			
		list($thumbwidth, $thumbheight) = explode('*', SIZE_JUZHAO_2);
		$result = image_resize($image_file, $image_file_2, $thumbwidth, $thumbheight);
		if(!$result && !is_file($image_file_2)) @copy($image_file, $image_file_2);
	}
	else if($cid == 3)
	{ // tou xiang
		list($thumbwidth, $thumbheight) = explode('*', SIZE_FACE_1);
		$result = image_resize($image_file, $image_file_1, $thumbwidth, $thumbheight, true);
		if(!$result && !is_file($image_file_1)) @copy($image_file, $image_file_1);
			
		list($thumbwidth, $thumbheight) = explode('*', SIZE_FACE_2);
		$result = image_resize($image_file, $image_file_2, $thumbwidth, $thumbheight);
		if(!$result && !is_file($image_file_2)) @copy($image_file, $image_file_2);
	}

	list($image_width, $image_height) = getimagesize($image_file);
	if($image_width < $image_height)
	{
		list($thumbwidth, $thumbheight) = explode('*', SIZE_IMAGE_MAX);
	}
	else
	{
		list($thumbheight, $thumbwidth) = explode('*', SIZE_IMAGE_MAX);
	}
	$result = image_resize($image_file, $image_name, $thumbwidth, $thumbheight);

	if(!is_file($image_name))
	{
		@copy($image_file, $image_name);
		$IoHandler->DeleteFile($image_file);
		$IoHandler->RemoveDir($cache_image_path);
	}
	
	return true;
}

function get_face_random($base_url='', $count=1)
{
	$face_image_path = ROOT_PATH.'data/images/face/default/';
	//$base_url = ($base_url == '') ?  'data/images/face/default/': $base_url;
	if(substr($base_url, -1) != '/')
	{
		$base_url .= '/';
	}
	$base_url .= 'data/images/face/default/';
	
	$min_image = 1;
	$max_image = 124;
	
	require_once ROOT_PATH.'lib/io.han.php';
	$IoHandler = new IoHandler();
	$files = $IoHandler->ReadDir($face_image_path);
	
	$images = array();
	if(check_array($files))
	{
		for($i=0; $i<$count; $i++)
		{
			$image_id = rand($min_image, $max_image);
			if(in_array($face_image_path.$image_id.'_1.jpg', $files) && !in_array($base_url.$image_id.'_1.jpg', $images))
			{
				$images['image_1'] = $base_url.$image_id.'_1.jpg';
				$images['image_2'] = $base_url.$image_id.'_2.jpg';
			}
		}
	}
	
	return $images;
}